package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.OrdersettingMapper;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    //上传文件后的业务层方法
    @Override
    public void add(List<OrderSetting> orderSettingsList) {
        if (orderSettingsList != null && orderSettingsList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingsList) {
               queryDateAndEditorAdd(orderSetting);
            }
        }
    }

    //根据日期查询判断是否存在信息方法
    public void queryDateAndEditorAdd(OrderSetting orderSetting){
        //检查该日期数据是否存在
        long count = ordersettingMapper.findCountByOrderDate(orderSetting);
        //如果该日期数据存在,则进行更新操作
        if (count > 0) {
            ordersettingMapper.updateNumberByOrderDate(orderSetting);
        }else {
            //如果不存在则执行新增操作
            ordersettingMapper.add(orderSetting);
        }
    }

    //编辑预约信息
    @Override
    public void editOrderSettingByDate(OrderSetting orderSetting) {
        queryDateAndEditorAdd(orderSetting);
    }

    //根据日期查询预约信息
    @Override
    public List<Map<Object, Object>> getOrderSettingByMonth(String date) {
        //设置开始日期和结束日期
        String dateBegin = date+"-1";
        String dateEnd = date + "-31";

        //创建map集合并添加数据
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("begin", dateBegin);
        hashMap.put("end",dateEnd);

        //根据传入的map集合来查询数据
        List<OrderSetting> orderSettingList = ordersettingMapper.getOrderSettingByMonth(hashMap);
        //创建集合,参数为map集合
        List<Map<Object, Object>> result = new ArrayList<>();
        //遍历集合
        for (OrderSetting orderSetting : orderSettingList) {
                //创建map集合
            Map<Object, Object> map = new HashMap<>();
            //向map集合中添加数据
            map.put("date", orderSetting.getOrderDate().getDate());//天数
            map.put("number", orderSetting.getNumber());//可预约的人数
            map.put("reservations", orderSetting.getReservations());//已预约的人数
            //将Map集合添加到list集合中
            result.add(map);
        }
        return result;
    }
}
